<?php
require_once("../../resources/commons.php");
require_once("../../data/User.php");
require_once("../../data/Report.php");
require_once("../../data/Item.php");

SessionManager::getInstance()->checkSession();
displayTop("Team overview","../../resources/ajaxscripts.php",null);

$l_myUserId=SessionManager::getCurrentLoggedUserId();
$l_user=User::getFromPrimaryKey($l_myUserId);
$l_teamMembers=$l_user->getFullTeam();
$l_totalExpenditures=Expense::getTotalExpensesForUser($l_myUserId);

if(count($l_teamMembers)!=0){
	$l_teamActivities=array();
	$l_teamWeeklyReports=array();
	$l_notProvidedReports=array();
	foreach($l_teamMembers as $l_teamMember){
		$l_teamActivities=array_merge($l_teamActivities,$l_teamMember->getOpenAndActiveActivities());
		$l_weeklyReport=$l_teamMember->getLastValidatedReport();
		if(isset($l_weeklyReport)){
			$l_teamWeeklyReports[$l_weeklyReport->getId()]=$l_weeklyReport;
		}
		$l_totalExpenditures=$l_totalExpenditures+Expense::getTotalExpensesForUser($l_teamMember->getId());
	}
	?>
	<h1>Budget status</h1>
	<?php echo($l_totalExpenditures);?> Euros
	<h1>Last published reports</h1>
	<?php
	if(count($l_teamWeeklyReports)!=0){
		$l_teamReportsTable=new TableData("TeamReportsTable");
		$l_teamReportsTable->addHeader("userFullName","Report from");
		$l_teamReportsTable->addHeader("createdDate","Created");
		$l_teamReportsTable->setZoomArea("../report/ajax_report_details.php");
		$l_teamReportsTable->setRecords($l_teamWeeklyReports);
		displayTable($l_teamReportsTable);
	}else{
		echo("None");
	}
	?>
	
<h1>Your team pending actions</h1>
	<?php
	$l_teamSummaryTable=new TableData("TeamSummaryTable");
	$l_teamSummaryTable->addHeader("firstName","First name");
	$l_teamSummaryTable->addHeader("lastName","Last name");
	$l_teamSummaryTable->addHeader("openAllocatedActionsCount","Open allocated actions");
	$l_teamSummaryTable->setZoomArea("ajax_team_overview_details.php");
	$l_teamSummaryTable->setRecords($l_teamMembers);
	displayTable($l_teamSummaryTable);
	?>

<h1>Activities of your team</h1>
	<?php
	if(count($l_teamActivities)!=0){
		$l_teamActivitiesTable=new TableData("TeamActivitiesTable");
		$l_teamActivitiesTable->addHeader("activityTypeLabel",$l_activityTypeHeaderLabel);
		$l_teamActivitiesTable->addHeader("title","Title");
		$l_teamActivitiesTable->addHeader("description","Description");
		$l_teamActivitiesTable->addHeader("responsibleUserFullName","Responsible");
		$l_teamActivitiesTable->setRecords($l_teamActivities);
		
		displayTable($l_teamActivitiesTable);
	}else{
		echo("none");
	}
}else{
	?>
	<div style="color: white;background-color: red;">No team</div>
	<?php
}

displayBottom();
?>